﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace ManageCategoriesProducts
{
    class ValidateCategory : ValidationRule
    {
        private string errorMessage;
        private int minLength;
        private int maxLength;

        public int MinLength
        {
            get { return minLength; }
            set { minLength = value; }
        }

        public int MaxLength
        {
            get { return maxLength; }
            set { maxLength = value; }
        }

        public string ErrorMessage
        {
            get { return errorMessage; }
            set { errorMessage = value; }
        }

        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);

            string inputString = value.ToString();
            if ((inputString.Length < this.minLength) || inputString.Length > this.maxLength) 
            {
                result = new ValidationResult(false, null);
            }
            return result;
        }
    }

    class ValidateProductName : ValidationRule
    {
        private string errorMessage;
        private int minLength;
        private int maxLength;

        public int MinLength
        {
            get { return minLength; }
            set { minLength = value; }
        }

        public int MaxLength
        {
            get { return maxLength; }
            set { maxLength = value; }
        }

        public string ErrorMessage
        {
            get { return errorMessage; }
            set { errorMessage = value; }
        }

        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);

            string inputString = value.ToString();
            if ((inputString.Length < this.minLength) || inputString.Length > this.maxLength)
            {
                result = new ValidationResult(false, null);
            }
            return result;
        }
    }

    class ValidateProductPrice : ValidationRule
    {       
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);
            double price = 0;
            if (!double.TryParse(value.ToString(), out price))
            {
                result = new ValidationResult(false, null);
                return result;
            }            
            if (price <= 0)
            {
                result = new ValidationResult(false, null);
            }
            return result;
        }
    }
}
